package org.keyyond.ms.business.util;

import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.service.MsSmsMessageLogService;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.sf.json.JSONObject;

@Component
public class CommonBeanutils {

	 @Autowired
	 MsSmsMessageLogService messageLogService;
	 
	 
	 
	 	/**
		 * 方法功能描述 记录日志
		 * 
		 * @author YCR
		 * @since 2018-1-23
		 * @category </br>方法功能描述 记录日志</br>
		 *           
		 * @param messageLog
		 *            |MsBusinessMessageLog | 必填
		 * @return <b>void</br>
		 * @exception 
		 */
	    public void addMessageLog(MsBusinessMessageLog messageLog) {
		messageLog.setCreateby(Constant.CONST_SYSTEM_LOG_CREATEON);
		messageLog.setModifyby(Constant.CONST_SYSTEM_LOG_MODIFYON);
		String log = JsonUtil.toJson(messageLog);
		String result = messageLogService.addMessageLog(log);
		if (result.contains(Constant.CONST_CHECK_RESULT_EXCEPTION)
			|| result.contains(Constant.CONST_CHECK_RESULT_FAIL)) {
			MsBusinessMessageLog newlog = new MsBusinessMessageLog();
		    newlog.setContent(messageLog.getContent());
		    newlog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
		    newlog.setReturnmessage(result);
		    newlog.setCreateby(Constant.CONST_SYSTEM_LOG_CREATEON);
		    newlog.setModifyby(Constant.CONST_SYSTEM_LOG_MODIFYON);
		    String resultLog = JsonUtil.toJson(newlog);
		    messageLogService.addMessageLog(resultLog);
		}
	    }

    /**
     * 对返回结果进行组装
     * @author YCR
     * @param
     * @return String
     * @throws
     */
	 /**
	  * 对返回结果进行组装
	  * 
	  * @author YCR
	  * @since 2018-1-23
	  * @category </br>对返回结果进行组装</br>
	  *           
	  * @param status
	  *            | boolean | 必填  状态
	  * @param message
	  *            | JSONObject | 必填  
	  * @return <b>{result:,data{message:}}</br>
	  * @exception 
	  */
    public String returnResult(boolean status, JSONObject message) {
	    JSONObject result = new JSONObject();
	    try {
		if (status) {
		    result.put(ConstantResult.RESULT_RESULT, true);
		    result.put(ConstantResult.RESULT_DATA, message);
		} else {
		    result.put(ConstantResult.RESULT_RESULT, false);
		    result.put(ConstantResult.RESULT_ERRMSG, message.getString(ConstantResult.RESULT_ERRMSG));
		    result.put(ConstantResult.RESULT_ERRCODE, message.getString(ConstantResult.RESULT_ERRCODE));
		}
	    }catch(Exception e) {
		    result.put(ConstantResult.RESULT_RESULT, false);
		    result.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_FORMAT_JSON);
		    result.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_FORMAT_JSON);
		e.printStackTrace();
	    }
	    return result.toString();
	}

}
